<template>
  <template v-if="route.path.startsWith('/login')">
    <router-view />
  </template>
  <BasicLayout v-else />
</template>

<script setup lang="ts">
import BasicLayout from "@/layouts/BasicLayout.vue";
import { useRoute, useRouter } from "vue-router";
import { useUserStore } from "@/store/UserStore";
import { Message } from "@arco-design/web-vue";
import { checkAccess } from "@/access/checkAccess";
import { onMounted } from "vue";

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

const doInit = () => {
  // 项目初始化代码
  console.log("欢迎来到我的项目");

  // 查看是否有登录记录
  const loginInfo = localStorage.getItem("loginInfo");
  if (loginInfo) {
    console.log((userStore.loginUser = JSON.parse(loginInfo)));
  }

  // 查看cookie中是否有SessionId
  // const sessionId = localStorage.getItem("JSESSIONID");
  // console.log("sessionId", sessionId);
};

onMounted(() => {
  doInit();
});

//#region 权限校验

// 路由守卫权限校验
router.beforeEach((to, from, next) => {
  // 如果to的mate中有access权限限制则进行判断
  if (to.meta.access) {
    //  如果userStore.loginUser.userRoles中有所需要的角色就放行
    if (checkAccess(to.meta.access)) {
      next();
    } else {
      // userStore.loginUser.userRoles.push("admin");
      Message.error("权限不足");
      next("/");
    }
  } else {
    next();
  }
});

//#endregion
</script>

<style scoped></style>
